package collector

import (
	"amq/conf"
	"bufio"
	"log"
	"os"

	"github.com/prometheus/client_golang/prometheus"
)

type amqCollector struct {
	count *prometheus.Desc
	tps   *prometheus.Desc
	diff  *prometheus.Desc
	// conf  *conf.Config
}

func (d *amqCollector) Describe(ch chan<- *prometheus.Desc) {
	ch <- d.count
	ch <- d.tps
	ch <- d.diff
}

func (d *amqCollector) Collect(ch chan<- prometheus.Metric) {
	// 打开文件
	file, err := os.Open(conf.C().FileConfig.Path)
	if err != nil {
		log.Fatalf("无法打开文件: %v", err)
	}
	defer file.Close()

	// 创建 Scanner
	scanner := bufio.NewScanner(file)
	lineNumber := 1

	for scanner.Scan() {
		line := scanner.Text()
		m := ParseLen(line)
		if m.Group != "#Group" {
			ch <- prometheus.MustNewConstMetric(d.count, prometheus.GaugeValue, m.IntCount(), m.Group, m.Version, m.Type, m.Model)
			ch <- prometheus.MustNewConstMetric(d.tps, prometheus.GaugeValue, m.IntTPS(), m.Group, m.Version, m.Type, m.Model)
			ch <- prometheus.MustNewConstMetric(d.diff, prometheus.GaugeValue, m.IntDiffTotal(), m.Group, m.Version, m.Type, m.Model)
		}
		lineNumber++
	}

}

func NewAmqCollector() *amqCollector {
	return &amqCollector{
		count: prometheus.NewDesc(
			"amq_count",
			"amq_count",
			// 动态标签的key列表
			[]string{"group", "version", "type", "model"},
			// 静态标签
			prometheus.Labels{"module": "amq"},
		),

		tps: prometheus.NewDesc(
			"amq_tps",
			"amq_tps",
			// 动态标签的key列表
			[]string{"group", "version", "type", "model"},
			// 静态标签
			prometheus.Labels{"module": "amq"},
		),
		diff: prometheus.NewDesc(
			"amq_diff",
			"amq_diff",
			// 动态标签的key列表
			[]string{"group", "version", "type", "model"},
			// 静态标签
			prometheus.Labels{"module": "amq"},
		),
	}

}
